package org.springblade.core.tool.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MyUtil {


    /**
     * 求和
     *
     * @param dataList
     * @return
     */
    public static Integer sumInt(List<Map<String, Object>> dataList, String key) {
        Integer sum = 0;
        Integer temp;
        Object v;
        if (dataList != null) {
            for (Map<String, Object> objectMap : dataList) {
                if (!objectMap.containsKey(key)) {
                    continue;
                }
                try {
                    v = objectMap.get(key);
                    if (v instanceof Integer) {
                        temp = (Integer) v;
                    } else {
                        temp = Double.valueOf(v.toString()).intValue();
                    }
                } catch (Exception ex) {
                    continue;
                }
                sum += temp;
            }
        }
        return sum;
    }

    /**
     * 求和
     *
     * @param dataList
     * @return
     */
    public static BigDecimal sumDouble(List<Map<String, Object>> dataList, String key) {
        Double sum = 0d;
        Double temp;
        Object v;
        if (dataList != null) {
            for (Map<String, Object> objectMap : dataList) {
                if (!objectMap.containsKey(key)) {
                    continue;
                }
                try {
                    v = objectMap.get(key);
                    if (v instanceof Double) {
                        temp = (Double) v;
                    } else {
                        temp = Double.valueOf(objectMap.get(key).toString());
                    }
                } catch (Exception ex) {
                    continue;
                }
                sum += temp;
            }
        }
        return NumberUtil.round(sum, 4);
    }


    /**
     * 转换为日期
     *
     * @param value
     * @return
     */
    public static Date toDate(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Date) {
            return (Date) value;
        }
        try {
            String v = value.toString();
            if (v.indexOf("-") <= 0) {
                if (v.length() == 8) {
                    return DateUtil.parse(v, "yyyyMMdd");
                } else if (v.length() >= 8) {
                    return DateUtil.parse(v.subSequence(0, 8), "yyyyMMdd");
                }
            } else {
                if (v.length() == 10) {
                    return DateUtil.parse(v, "yyyy-MM-dd");
                } else if (v.length() > 10) {
                    return DateUtil.parse(v.subSequence(0, 10), "yyyy-MM-dd");
                }
            }
        } catch (Exception ex) {
        }
        return null;
    }

    /**
     * 转换为日期时间
     *
     * @param value
     * @return
     */
    public static Date toDatetime(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Date) {
            return (Date) value;
        }
        try {
            return DateUtil.parse(value.toString());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 转换为长整型
     *
     * @param value
     * @return
     */
    public static Long toLong(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        if (value instanceof Integer) {
            return Long.valueOf((Integer) value);
        }
        if (value instanceof Double) {
            return ((Double) value).longValue();
        }
        if (value instanceof Float) {
            return ((Float) value).longValue();
        }
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).longValue();
        }
        try {
            String v = value.toString();
            if (v.length() > 0) {
                return Long.valueOf(v.trim());
            }
        } catch (Exception ex) {
        }
        return null;
    }

    /**
     * 转换为整型
     *
     * @param value
     * @return
     */
    public static Integer toInteger(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof Long) {
            return ((Long) value).intValue();
        }
        if (value instanceof Double) {
            return ((Double) value).intValue();
        }
        if (value instanceof Float) {
            return ((Float) value).intValue();
        }
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).intValue();
        }
        try {
            String v = value.toString();
            if (v.length() > 0) {
                return Integer.valueOf(v.trim());
            }
        } catch (Exception ex) {
        }
        return null;
    }

    /**
     * 转换为浮点型
     *
     * @param value
     * @return
     */
    public static BigDecimal toBigDecimal(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (value instanceof Integer) {
            return BigDecimal.valueOf((Integer) value);
        }
        if (value instanceof Long) {
            return BigDecimal.valueOf((Long) value);
        }
        if (value instanceof Double) {
            return BigDecimal.valueOf((Double) value);
        }
        if (value instanceof Float) {
            return BigDecimal.valueOf((Float) value);
        }
        try {
            String v = value.toString();
            if (v.length() > 0) {
                return BigDecimal.valueOf(Double.valueOf(v.trim()));
            }
        } catch (Exception ex) {
        }
        return null;
    }

    /**
     * 转换
     *
     * @param value
     * @return
     */
    public static String toString(Object value) {
        if (value == null) {
            return null;
        }
        return value.toString().trim();
    }

}
